<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <ul>
            <!-- 如果当前列表是循环出来的，并且有事件 -->
            <li v-for="item in list">
                <input v-model="item.check" type="checkbox">
                商品名称：{{item.title}}--
                商品价格：{{item.price}}--
                <button @click="delItem(item)">减</button>
                商品数量：{{item.count}}
                <button @click="addItem(item)">加</button>

                商品小计：{{item.count * item.price}}


            </li>
        </ul>
        <div>
            <p>总数量:{{allCount}}</p>

            <p>总计:{{allPrice}}</p>
            <p>全选<input @change=" changeFlag" v-model="allCheck" type="checkbox"></p>
        </div>

    </div>
    <script src="./vue.min.js"></script>
    <script>
        new Vue({
            el: "#app",
            data: {
                allCheck: false,
                list: [
                    {
                        title: "苹果",
                        price: 100,
                        check: false,
                        count: 2

                    },
                    {
                        title: "华为",
                        price: 200,
                        check: false,
                        count: 1

                    },
                    {
                        title: "小米",
                        price: 300,
                        check: false,
                        count: 3

                    },
                ]

            },
            //计算属性的作用：缓存
            computed: {
                allCount() {
                    return this.list.reduce((next, prev) => {
                        //如果是选中
                        if (prev.check) {
                            return next + prev.count

                        } else {
                            return next
                        }

                    }, 0)
                },
                allPrice() {
                    return this.list.reduce((next, prev) => {
                        if (prev.check) {
                            return next + prev.count * prev.price
                        } else {
                            return next
                        }

                    }, 0)
                },

            },
            methods: {
                addItem(item) {
                    item.count += 1
                },
                delItem(item) {
                    item.count -= 1
                },
                changeFlag() {
                    this.list.forEach(item => {
                        item.check = this.allCheck
                    })
                }

            },
            watch: {
                list: {
                    deep: true,
                    handler(value) {
                        this.allCheck = every(item => item.check)
                        console.log(flag);

                    }
                },
                allCheck(newValue) {
                    this.list.forEach(item => {
                        item.check = newValue
                    })


                    console.log(newValue);
                }

            }

        })
    </script>
</body>

</html>